package com.dsr.controller;

import com.dsr.consts.Result;
import com.dsr.consts.e.BaseProperty;
import com.dsr.consts.e.UserProperty;
import com.dsr.entity.User;
import com.dsr.service.UserService;
import com.dsr.util.BasicUtil;
import com.dsr.util.JwtUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * (User)表控制层
 *
 * @author dsr
 * @since 2020-08-27
 */
@Api(tags = "(User)表控制层")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("更具id获取实体")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "id", required = true)
    })
    @GetMapping("/get/{id}")
    public Result getEntity(@PathVariable String id) {
        return userService.getEntity(id);
    }

    @ApiOperation("保存操作")
    @ApiParam(name = "user", value = "实体", required = true)
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user);
    }

    @ApiOperation("更新操作")
    @ApiParam(name = "user", value = "实体", required = true)
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        return userService.update(user);
    }

    @ApiOperation("根据id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id(ps:1,2,3)", required = true)
    })
    @DeleteMapping("/delete")
    public Result delete(@RequestBody String id) {
        return userService.delete(id);
    }

    @ApiOperation("查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "页", required = true),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页大小", required = true),
    })
    @ApiParam(name = "user", value = "查询字段", required = true)
    @PostMapping("/list/{page}/{size}")
    public Result query(@PathVariable int page, @PathVariable int size, @RequestBody User user) {
        return userService.query(page, size, user);
    }

    @ApiOperation("返回所有")
    @GetMapping("/list")
    public Result list() {
        return userService.list();
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        return userService.login(user);
    }

    @ApiOperation("验证登录")
    @GetMapping("/login/{id}")
    public Result checkLogin(@PathVariable String id){
        return userService.checkLogin(id);
    }

    @ApiOperation("验证登录(伪登录)")
    @GetMapping("/login")
    public Result getAuthorization(){
        Map<String,String> map=new HashMap<>();
        map.put(BaseProperty.ROLE, UserProperty.USER);
        BasicUtil.setCookie("token",JwtUtil.getToken(map));
        return Result.success("请求成功");
    }
}